﻿using System;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace DL.UIBinding
{
    /// <summary>
    /// 轮播图数据基类
    /// 需要图片
    /// 点击跳转（可选）
    /// 有效时间
    /// </summary>
    public class CarouselContext
    {
        protected int m_Index;

        public int Index
        {
            get => m_Index;
        }
        
        protected float m_ActiveTimer;
        protected string m_Icon;
        protected Action OnClick;
        
        //是否常驻
        private bool m_IsPermanent;
    }

    public class CarouselCollection<T> : ICarouselCollection<T> where T : CarouselContext
    {
        private readonly List<T> m_Items = new List<T>();
        private CarouselView m_CarouselView;
        public CarouselCollection([NotNull] CarouselView carouselView)
        {
            m_CarouselView = carouselView;
        }

        public int Count => m_Items.Count;
        
        //数据操作方法  Add 和 Insert Remove 方法 
        
        public void Add(T item) => Insert(item, this.m_Items.Count);
        
        public void Insert(T item, int index)
        {
            if (index < 0 || index >= this.m_Items.Count)
                index = this.m_Items.Count;
            this.m_Items.Insert(index, item);
            
            //如果插在中间，那么它和它之后的都需要重新计算
            Refresh(index);
        }
        
        //移除
        public void Remove(int index)
        {
            if (m_Items.Count <= index) return;
            var item = m_Items[index];
            m_Items.Remove(item);
            Refresh(index);
        }
        
        public void Remove(T item)
        {
            for (int index = 0; index < this.m_Items.Count; ++index)
            {
                if ((object)this.m_Items[index] == (object)item)
                {
                    this.Remove(index);
                    break;
                }
            }
        }

        //刷新后面的数据，包括自己
        private void Refresh(int index = 0)
        {
            for (int i = index; i < m_Items.Count; i++)
            {
                var item = GetItem(i);
                //后面的数据信息都是错的
            }
            
            //准备通知视图去更新显示  
            // Debug.Log("收集器数量改变，通知视图更新");
        }

        public T GetItem(int index) => index < 0 || index >= this.m_Items.Count ? default(T) : this.m_Items[index];

    }

    
    public interface ICarouselCollection<out T> where T : CarouselContext
    {
        
    }
}